import axios from 'axios'
const key = 'JVJBZ-FZJCZ-D43XV-ZMFWK-I3462-DTBPL'

class MapService {
  async geocoder(address) {
    try {
      const response = await axios.get('https://apis.map.qq.com/ws/geocoder/v1', {
        params: { address, key }
      })
      return response.data
    } catch (error) {
      throw new Error(`地理编码失败: ${error.message}`)
    }
  }

  async distance(from, to) {
    try {
      const response = await axios.get('https://apis.map.qq.com/ws/distance/v1/matrix', {
        params: {
          mode: 'bicycling',
          from,
          to,
          key
        }
      })
      return response.data
    } catch (error) {
      throw new Error(`距离计算失败: ${error.message}`)
    }
  }

  async placeSuggestion(keyword) {
    try {
      const response = await axios.get('https://apis.map.qq.com/ws/place/v1/suggestion', {
        params: { keyword, key }
      })
      return response.data
    } catch (error) {
      throw new Error(`地点建议失败: ${error.message}`)
    }
  }

  async placeSearch(keyword) {
    try {
      const response = await axios.get('https://apis.map.qq.com/ws/place/v1/search', {
        params: {
          boundary: "region(郑州)",
          keyword,
          key
        }
      })
      return response.data
    } catch (error) {
      throw new Error(`地点搜索失败: ${error.message}`)
    }
  }
}

const mapService = new MapService()
export default mapService 